package com.example.demo1.service.impl;

import com.example.demo1.mapper.UserMapper;
import com.example.demo1.pojo.User;
import com.example.demo1.service.UserService;
import com.example.demo1.utils.Md5Util;
import com.example.demo1.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User findByName(String name) {

        User u = userMapper.findByName(name);
        return u;
    }

    @Override
    public void register(String name, String password) {
        String md5String = Md5Util.string2MD5(password);
        userMapper.add(name,md5String);
    }

    @Override
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }

//    @Override
//    public void updaeAvatar(String avatarUrl) {
//        Map<String,Object> user = ThreadLocalUtil.get();
//        Integer id = (Integer)user.get("id");
//        userMapper.updateAvatar(avatarUrl,id);
//    }

    @Override
    public void updatePwd(String newPwd) {
        Map<String,Object> user = ThreadLocalUtil.get();
        Integer id = (Integer)user.get("id");
        userMapper.updatePwd(Md5Util.string2MD5(newPwd),id);
    }

    @Override
    public String upload(MultipartFile file, Integer id) {
        FileOutputStream fos = null;
        InputStream fis = null;
        byte[] bytes = new byte[20];

        //System.out.println("添加图片 " + id + " " + file.getName());
        String filePath=null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd_HH.mm.ss");
        Date date = new Date();
        String currentTime = sdf.format(date);

        try {
            fos = new FileOutputStream("C:\\Users\\jizhaoyang\\Documents\\idea_project\\webproject\\demo1\\src\\main\\resources\\static\\userPic\\"  + currentTime + "_Message_" + id + ".jpg");

            fis = file.getInputStream();
            int read = fis.read(bytes);
            while (read != -1) {
                fos.write(bytes, 0, bytes.length);
                read = fis.read(bytes);
            }
            filePath = userMapper.uploadSelect(id);
            //System.out.println(filePath);
            if(filePath!=null){
                File deleteFile = new File(filePath);
                String fileName = deleteFile.getName();
                deleteFile = new File("C:\\Users\\jizhaoyang\\Documents\\idea_project\\webproject\\demo1\\src\\main\\resources\\static\\userPic\\" + fileName);
                if(deleteFile.exists() && !fileName.equals("default.jpeg")){
                    deleteFile.delete();
                    System.out.println("文件已成功删除");
                }
            }
            filePath = "http://localhost:8080/userPic/" + currentTime + "_Message_" + id + ".jpg";
            //System.out.println(filePath+"  "+id);
            userMapper.upload(filePath, id);
        } catch (Exception e) {
            System.out.println(e);
            return "500";
        } finally {
            try {
                fos.close();
                fis.close();
            } catch (Exception e) {
                System.out.println(e);
                return "500";
            }
        }
        return filePath;
    }
}
